package com.market.cash.utils.event

import com.market.cash.http.ApiService
import com.market.cash.http.ApiService.addCommonParams
import com.market.cash.utils.CashRootAesUtil
import com.market.cash.utils.log.CashRootLog
import com.market.cash.utils.log.JsonFormat
import com.market.network.OkHttp
import com.market.network.callback.StringCallback
import com.market.network.model.Response
import org.json.JSONObject

/**
 * CashRoot
 * @Author wwdeng
 * @CreateTime: 2024/10/17 19:38
 * @Annotation：埋点事件处理类
 */
object CashRootEventHelper {

    /**
     * 添加打点事件
     */
    fun addCashRootRequestEventAction(eventName: String, remark: String = "") {
        CashRootLog.e(">>>>>>>>>>>>>>eventName：$eventName，remark：$remark")
//        if (CashRootLog.isDebug) return

        val eventType = eventName.uppercase()//转大写
        val httpUrl = ApiService.getHttpUrl(ApiService.API_URL.CASHROOT_API_ADD_POINT_URL)
        val params = JSONObject().addCommonParams().apply {
            put("path", ApiService.API_PATH.CASHROOT_API_TRACK_EVENT_PATH)
            put("type",eventType)//事件类型
            put("remark",remark)//事件备注
        }

        val aesParams = CashRootAesUtil.encryptDefault(params.toString())
        CashRootLog.e("event url：  $httpUrl")
        CashRootLog.e("event params： $params")
        CashRootLog.e("event params AES：  $aesParams")
        OkHttp.post<String>(httpUrl) // 请求方式和请求url
            .tag(this) // 请求的 tag, 主要用于取消对应的请求
            .upString(aesParams)
            .execute(object : StringCallback() {
                override fun onSuccess(response: Response<String>) {
                    val result = CashRootAesUtil.decryptDefault(response.body())
                    Thread { CashRootLog.i("event Response：${JsonFormat.format(result)}") }.start()
                }
                override fun onError(response: Response<String>) {
                }
                override fun onFinish() {
                }
            })
    }

}